define [
  'handlebars'
  'chaplin'
  'lib/utils'
], (Handlebars, Chaplin, utils) ->

  'use strict'

  # Application-specific Handlebars helpers
  # =============================================================

  # Get Chaplin-declared named routes. {{#url "like" "105"}}{{/url}}
  Handlebars.registerHelper 'url', (routeName, params..., options) ->
    utils.reverse routeName, params

  # Mask Decimal
  # ------------------
  Handlebars.registerHelper 'maskDecimal', (value) =>
    return '0,00' unless value
    return value.toFixed(2).replace '.', ','

  # Coloca zeros à esquerda
  # http://www.electrictoolbox.com/pad-number-zeroes-javascript/
  # ------------------
  Handlebars.registerHelper 'leadingZero', (number, length=2) =>
    str = ''
    if number? && number != ""
      str = "#{number}"
      while str.length < length
        str = '0' + str

    new Handlebars.SafeString str

  # Inclui o conteúdo de um template externo.
  # PS: O template deve estar listado nas dependências da view que for usa-lo
  # http://www.electrictoolbox.com/pad-number-zeroes-javascript/
  # ------------------
  Handlebars.registerHelper 'loadTemplate', (options) ->
    file = options.hash?.file
    fileContent = require "text!" + file
    return new Handlebars.SafeString(fileContent)

  # Retorna os elementos como HTML
  # ------------------
  Handlebars.registerHelper 'html', (content='', args={}) =>
    template = Handlebars.compile content
    new Handlebars.SafeString template args

  null
